import { getAccessTypeEnumSchema } from '@/enums/accessTypeEnum';
import { AdColumn } from '@/components/AdVxeTable';
import { TreeItem } from '@/components/Tree';
import { useI18n } from '@/hooks/web/useI18n';

const accessTypeEnumSchema = getAccessTypeEnumSchema();
const { t } = useI18n();

export const columns: AdColumn[] = [
  {
    title: t('entity.idTitle'),
    field: 'Id',
    width: 180,
    visible: false,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'integer',
  },
  {
    title: t('routes.system.identity.role.nameTitle'),
    field: 'Name',
    width: 200,
    formVisible: true,
    formEditable: true,
    required: true,
    filterable: true,
    dataType: 'string',
    sortable: true,
  },
  {
    title: t('routes.system.identity.role.descriptionTitle'),
    field: 'Description',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'string',
    formSchema: {
      colProps: {
        // span: 24,
        xs: 24, // <576px
        sm: 24, // ≥576px
        md: 24, // ≥768px
        lg: 24, // ≥992px
        xl: 24, // ≥1200px
        xxl: 24, // ≥1600px
        // xxxl: 24, // ≥2000px 3.0
      },
    },
  },
  {
    title: t('routes.system.identity.role.remarkTitle'),
    field: 'Remark',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'text',
    formSchema: {
      colProps: {
        // span: 24,
        xs: 24, // <576px
        sm: 24, // ≥576px
        md: 24, // ≥768px
        lg: 24, // ≥992px
        xl: 24, // ≥1200px
        xxl: 24, // ≥1600px
        // xxxl: 24, // ≥2000px 3.0
      },
    },
  },
  {
    title: t('routes.system.identity.role.isAdminTitle'),
    field: 'IsAdmin',
    width: 90,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'boolean',
    sortable: true,
    formSchema: {
      colProps: {
        // span: 8,
        xs: 24, // <576px
        sm: 24, // ≥576px
        md: 12, // ≥768px
        lg: 12, // ≥992px
        xl: 8, // ≥1200px
        xxl: 8, // ≥1600px
        // xxxl: 8, // ≥2000px 3.0
      },
    },
  },
  {
    title: t('routes.system.identity.role.isDefaultTitle'),
    field: 'IsDefault',
    width: 70,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'boolean',
    sortable: true,
    formSchema: {
      colProps: {
        // span: 8,
        xs: 24, // <576px
        sm: 24, // ≥576px
        md: 12, // ≥768px
        lg: 12, // ≥992px
        xl: 8, // ≥1200px
        xxl: 8, // ≥1600px
        // xxxl: 8, // ≥2000px 3.0
      },
    },
  },
  {
    title: t('entity.lockable.isLockedTitle'),
    field: 'IsLocked',
    width: 70,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'boolean',
    sortable: true,
    formSchema: {
      colProps: {
        // span: 8,
        xs: 24, // <576px
        sm: 24, // ≥576px
        md: 12, // ≥768px
        lg: 12, // ≥992px
        xl: 8, // ≥1200px
        xxl: 8, // ≥1600px
        // xxxl: 8, // ≥2000px 3.0
      },
    },
  },
  {
    title: t('entity.creationAudited.creatorIdTitle'),
    field: 'CreatorId',
    width: 0,
    visible: false,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'integer',
  },
  {
    title: t('entity.creationAudited.creatorNameTitle'),
    field: 'CreatorName',
    width: 100,
    filterable: true,
    formVisible: true,
    formEditable: false,
    filterIndex: 'CreatorId',
    filterJoin: {
      entityType: 'MicroCloud.Pack.Identity.Entities.User,MicroCloud.Pack.Identity',
      filterField: 'UserName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.creationAudited.creatorNicknameTitle'),
    field: 'CreatorNickname',
    width: 100,
    formVisible: true,
    formEditable: false,
    filterable: true,
    filterIndex: 'CreatorId',
    filterJoin: {
      entityType: 'MicroCloud.Pack.Identity.Entities.User,MicroCloud.Pack.Identity',
      filterField: 'NickName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.creationAudited.createdTimeTitle'),
    field: 'CreatedTime',
    width: 180,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'datetime',
    sortable: true,
  },
  {
    title: t('entity.updateAudited.updaterIdTitle'),
    field: 'UpdaterId',
    width: 0,
    visible: false,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'integer',
  },
  {
    title: t('entity.updateAudited.updaterNameTitle'),
    field: 'UpdaterName',
    width: 100,
    filterable: true,
    formVisible: true,
    formEditable: false,
    filterIndex: 'UpdaterId',
    filterJoin: {
      entityType: 'MicroCloud.Pack.Identity.Entities.User,MicroCloud.Pack.Identity',
      filterField: 'UserName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.updateAudited.updaterNicknameTitle'),
    field: 'UpdaterNickname',
    width: 100,
    formVisible: true,
    formEditable: false,
    filterable: true,
    filterIndex: 'UpdaterId',
    filterJoin: {
      entityType: 'MicroCloud.Pack.Identity.Entities.User,MicroCloud.Pack.Identity',
      filterField: 'NickName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.updateAudited.updatedTimeTitle'),
    field: 'UpdatedTime',
    width: 180,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'datetime',
    sortable: true,
  },
];

export const roleFunctionColumns: AdColumn[] = [
  {
    title: t('routes.system.authorization.functionIdTitle'),
    field: 'Id',
    width: 0,
    visible: false,
    filterable: false,
    dataType: 'integer',
  },
  {
    title: t('routes.system.authorization.functionNameTitle'),
    field: 'Name',
    width: 300,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('routes.system.authorization.functionAccessTypeTitle'),
    field: 'AccessType',
    width: 100,
    filterable: true,
    dataType: 'enum',
    enumData: accessTypeEnumSchema,
  },
  {
    title: t('routes.system.authorization.functionAreaTitle'),
    field: 'Area',
    width: 150,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('routes.system.authorization.functionControllerTitle'),
    field: 'Controller',
    width: 200,
    filterable: true,
    dataType: 'string',
  },
];

/**
 * 将原始树数据节点转换为 `TreeItem`节点
 * @param nodes 原始树数据节点
 * @param checkedKeys 选中值
 */
export function makeRoleModulesTree(nodes: any[], checkedKeys: (string | bigint)[]): TreeItem[] {
  if (!nodes.length) {
    return [];
  }
  const result: TreeItem[] = [];
  for (const node of nodes) {
    const icon = node.HasChildren
      ? 'fluent:text-bullet-list-tree-24-filled'
      : node.HasAuth
        ? 'emojione-v1:white-heavy-check-mark'
        : 'noto-v1:check-mark-button';
    // const hasAuth = node.HasChildren ? false : node.HasAuth;
    // const title = `${node.FullName.replace('根节点-', '')}${hasAuth ? '(已授权)' : ''} --> ${
    //   node.Code
    // }`;
    let title = node.FullName;
    const fullNameArr = node.FullName.split('-');
    if (fullNameArr?.length > 1) {
      title = node.FullName.replace(`${fullNameArr[0]}-`, '');
    }

    const item: TreeItem = {
      icon: icon,
      key: node.Id,
      title: title,
      checked: node.IsChecked,
      isLeaf: !node.HasChildren,
    };
    if (node.IsChecked) {
      checkedKeys.push(node.Id);
    }
    if (node.Children && node.Children.length > 0) {
      item.children = makeRoleModulesTree(node.Children, checkedKeys);
    }
    result.push(item);
  }
  return result;
}
